﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{

    public class YGRVKDOCData : BaseFunction
    {
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public string UserID
        {
            get
            {
                return app.User;
            }
        }
        SETINTEXDB INTEXDB = new SETINTEXDB();
      
        public string UserName
        {
            get
            {
                return app.User;
            }
        }
        public YGRVKDOCData()
            : base()
        {
            Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public DataSet LoadEnv()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            try
            {
            DataRow dwData = null;
            dwData = INTEXDB.ICC_WFEVENTDEF.NewRow();
            dwData["MANDANT"] = "00";
            dwData["WFCLASS"] = "901";
            SResult = INTEXDBC.QueryICC_WFEVENTDEF(dwData, 2);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["WFEDES"] = Helpers.Helper.GetGB2312Value(row["WFEDES"].ToString().Trim());
            }
            SReturn.Merge(SResult);
            dwData = INTEXDB.SDDAT.NewRow();
            dwData["SDSA"] = "0013";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0114";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0734";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0241";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0208";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0209";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999)); 
            dwData["SDSA"] = "0477";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            SReturn.Merge(INTEXDBC.QueryHRSTAMM(dwData, 3));
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet QueryWFEVENTS(DataRow dwInput)
        {

            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = INTEXDB.ICC_WFEVENTS.NewRow();
                dwData["MANDANT"] = dwInput["MANDANT"];
                dwData["WFEVENTS"] = dwInput["WFEVENTS"];
                dwData["CONTROL"] = "NO ";
                dwData["WFEUSER"] = dwInput["WFEUSER"];
                SResult = INTEXDBC.QueryICC_WFEVENTS(dwData, 5);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            
            return SReturn;
        }
        public DataSet BuildData(string client,string KRS, string LFD)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            string VKPAUFART = "";
            try
            { 
                SResult = INTEXBL.QueryVKK(MANDANT, KRS, LFD);
                if(SResult.Tables.Contains("VKKOPF"))
                {
                    foreach (DataRow row in SResult.Tables["VKKOPF"].Rows)
                    {
                        foreach (DataRow rp in SResult.Tables["GLOPITEM"].Rows)
                        {
                            row["VKKKOMENGE"] = rp["GLOAMICUC"];
                        }
                    }
                }
                if (SResult.Tables.Contains("VKPOS"))
                {
                    foreach (DataRow row in SResult.Tables["VKPOS"].Rows)
                    {
                        VKPAUFART = row["VKPAUFART"].ToString();
                        row["VKPZUDOPA"] = row["VKPZUSATZ"].ToString().Substring(0, 7);
                        row["VKPZUSATZ"] = row["VKPZUSATZ"].ToString().Substring(7);
                    }
                }
                SReturn.Merge(SResult);
                if (VKPAUFART == "NA")
                {
                    ManageTXT(ref SReturn, KRS, LFD);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            return SReturn;
        }
        private void ManageTXT(ref DataSet src, string KRS, string LFD)
        {
            DataTable tb = src.Tables["ICC_TXTKON"];
            string IVDI = "";
            if (tb == null)
                return;
            DataRow dwIVDI=null;
            foreach (DataColumn cm in tb.Columns)
            {
                string cname = cm.ColumnName;
                foreach (DataRow row in src.Tables["ICC_TXTKON"].Rows)
                {
                    row[cname] = Helper.Convert8859P1ToGB2312(row[cname].ToString());
                    if (cname == "TXTIPIND")
                    {
                        if (row[cname].ToString().Contains("IVDI"))
                        {
                            dwIVDI=row;
                            IVDI = row[cname].ToString();
                        }
                    }
                }
            }
            #region===初始化==============

            if (IVDI.Trim() == "")
            {
                DataRow row = tb.Rows.Add();
                row["TXTIPIND"] = KRS + LFD + "000IVDI                                             ";
                row["TXT010"] = "确认可达标";
                row["TXT010HR"] = "        ";
                row["TXT010DT"] = "       0";
                row["TXT020"] = "颜色确认,复板周期__天";
                row["TXT020HR"] = "        ";
                row["TXT020DT"] = "      0";
                row["TXT030"] = "有原料，__月__日下复板单，__月__日下混棉单";
                row["TXT030HR"] = "        ";
                row["TXT030DT"] = "       0";
                row["TXT040"] = "运输周期___天，到货______________";
                row["TXT040HR"] = "        ";
                row["TXT040DT"] = "       0";
                row["TXT050"] = "交期:___月___日交完，交货周期____天";
                row["TXT050HR"] = "        ";
                row["TXT050DT"] = "       0";
                row["TXT060"] = "确认可按计划完成";
                row["TXT060HR"] = "        ";
                row["TXT060DT"] = "       0";
                row["TXT070"] = "";
                row["TXT070HR"] = "        ";
                row["TXT070DT"] = "       0";
                for (int i = 1; i <= 7; i++)
                {
                    string position = Right("000" + i * 10, 3);
                    SaveText(src, KRS, LFD, "000", position, "        ", "0       ");
                }
            }
            #endregion
            #region=======ICC_WFEVENTS========     
            foreach (DataRow row in src.Tables["ICC_WFEVENTS"].Rows)
            {
                string txtposition = "010";
                switch (row["WFEVENTS"].ToString().Trim())
                {
                    case "MYGRVKPOS001"://品质审核  
                        txtposition = "010";
                        break;
                    case "MYGRVKPOS002"://颜色审核 
                        txtposition = "020";
                        break;
                    case "MYGRVKPOS003"://计划备料审核 
                        txtposition = "030";
                        break;
                    case "MYGRVKPOS004"://运输 
                        txtposition = "040";
                        break;
                    case "MYGRVKPOS005"://计划
                        txtposition = "050";
                        break;
                    case "MYGRVKPOS006"://生产  
                        txtposition = "060";
                        break;
                    case "MYGRVKPOS007"://销售
                        txtposition = "070";
                        break;
                }
                dwIVDI["TXT"+txtposition+"HR"] = row["CHGUSER"].ToString();
                dwIVDI["TXT"+txtposition+"DT"] =Left(row["CHGTIME"].ToString(),8);
            }
            #endregion
            
        }
        public string Today
        {
            get
            {
                DateTime now = System.DateTime.Now;
                string year = now.Year.ToString();
                string m = Right("00" + now.Month.ToString(), 2);
                string day = Right("00" + now.Day.ToString(), 2);
                return year + m + day;
            }
        }
        public override DataSet LogIn(string MANDANT, string UsingID, System.Security.SecureString sPassword)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult;
            DataTable tableLocal = null;
            try
            {
                IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(sPassword);
                string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
                UsingID = UsingID.Trim().ToUpper();
                Password = Password.Trim().ToUpper();
                DataRow dwTemp = INTEXDB.HRSTAMM.NewRow();
                dwTemp["HRMANDANT"] = MANDANT;
                dwTemp["HRBENID"] = Left(UsingID + "                    ", 8);
                dwTemp["HRDFAX"] = Left(Password + "                    ", 20);
                SResult = INTEXDBC.QueryHRSTAMM(dwTemp, 1);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return SReturn;
        }
        public void SaveText(DataSet src, string KRS, string LFD,string POS,string txtposition,string _user,string _date)
        {
            Helper.GetDate(ref date, ref year, ref month, ref day);
            DataSet SReturn = new DataSet();
            DataSet SResult;
            decimal TXTIPIND = 1;
            DataTable tb = src.Tables["ICC_TXTKON"];
            if (tb == null)
                return;
            string VKPAUF = KRS + LFD;
            string TXTISIND = KRS + LFD + POS+"IVDI                                             ";
            try
            {
                DataRow dwData = INTEXDB.TXTIND.NewRow();
                dwData["TXTIMANDANT"] = "00";
                dwData["TXTITXTART"] = "VKT";
                dwData["TXTISIND"] = TXTISIND;
                SResult = INTEXDBC.QueryTXTIND(dwData, 2);
                if (SResult.Tables[0].Rows.Count == 0)
                {
                    SResult = INTEXDBC.QueryTXTIND(dwData, 3);
                    if (SResult.Tables.Count > 0)
                    {
                        TXTIPIND = System.Convert.ToDecimal(SResult.Tables[0].Rows[0]["TXTIPIND"].ToString()) + 1;
                        dwData["TXTISPRACHKZ"] = "  ";
                        dwData["TXTIPIND"] = TXTIPIND;
                        dwData["TXTITITEL"] = "                              ";
                        dwData["TXTINURERGAENZ"] = " ";
                        dwData["TXTISAPTEXTART"] = "    ";
                        dwData["TXTISTARTENDE"] = " ";
                        dwData["TXTIKZPERS"] = " ";
                        //dwData["TXTIAENDBEN"] = _user;
                        //dwData["TXTIAENDDAT"] = _date;
                        dwData["TXTIAENDZL"] = 0;
                        dwData["TXTILPOS"] = 0;
                        dwData["TXTIRICHEDIT"] = " ";
                        dwData["TXTIFILLER"] = "                    ";
                        dwData["TXTINEUBEN"] = _user;
                        dwData["TXTINEUDAT"] = _date;
                        dwData["TXTIUPDT"] = "J";
                        INTEXDBC.SaveTXTIND(dwData);
                    }
                }
                else
                {
                    TXTIPIND = System.Convert.ToDecimal(SResult.Tables[0].Rows[0]["TXTIPIND"].ToString());
                }
                foreach (DataRow dwTXT in tb.Rows)
                {
                    if (!dwTXT["TXTIPIND"].ToString().Contains("IVDI"))
                        continue;
                    
                    for (int i = 1; i <= 39; i++)
                    {
                        string position = Right("000" + i * 10, 3);
                        if (position != txtposition)
                            continue;
                        dwTXT["TXT"+position+"HR"] =_user;
                        dwTXT["TXT"+position+"DT"] = _date;
                        string text = dwTXT["TXT" + position].ToString();
                        //if (text.Trim() == "") continue;
                        dwData = INTEXDB.TXTKON.NewRow();
                        dwData["TXTKSL"] = 30;
                        dwData["TXTKFILLER"] = "  ";
                        dwData["TXTKMANDANT"] = "00";
                        dwData["TXTKPIND"] = TXTIPIND;
                        dwData["TXTKPOS"] = i * 10;
                        dwData["TXTKATR"] = 0;
                        dwData["TXTKTEXT"] = Left(Helper.ConvertGB2312To8859P1(text).PadRight(80), 80);
                        INTEXDBC.SaveTXTKON(dwData);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet GetVV(string client, string KRS, string LFD)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();

            try
            {
                SResult = INTEXBL.QueryVV(MANDANT, KRS, LFD); 
                SReturn.Merge(SResult);  
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            } 
            return SReturn;
        }
    }
}